package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.BuyRequestbuyBillItem;
import com.ruoyi.buy.domain.entity.PO.InvInventoryInfo;
import com.ruoyi.buy.domain.entity.PO.InvTransforminvBill;
import com.ruoyi.buy.domain.entity.PO.InvTransforminvBillItem;
import com.ruoyi.buy.domain.entity.buy0011.Buy0011TableData;
import com.ruoyi.buy.mapper.PO.BuyRequestbuyBillItemMapper;
import com.ruoyi.buy.mapper.PO.InvInventoryInfoMapper;
import com.ruoyi.buy.mapper.PO.InvTransforminvBillItemMapper;
import com.ruoyi.buy.mapper.PO.InvTransforminvBillMapper;
import com.ruoyi.buy.mapper.PO.inv0015.inv0015Mapper;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.buy.service.IInv0015Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class Inv0015ServiceImpl implements IInv0015Service
{

    @Resource
    private inv0015Mapper inv0015Mapper;

    @Autowired
    ICommonService commonService;

    @Resource
    InvTransforminvBillMapper invTransforminvBillMapper;

    @Resource
    InvInventoryInfoMapper invInventoryInfoMapper;

    @Resource
    InvTransforminvBillItemMapper invTransforminvBillItemMapper;

    @Resource
    BuyRequestbuyBillItemMapper requestbuyBillItemMapper;

    @Override
    public List<Buy0011TableData> getList(Buy0011TableData param) {
        return this.inv0015Mapper.getList(param);
    }

    @Override
    public void invTransform(Buy0011TableData param) throws Exception {
        //这个方法是一般库存转项目库存
        InvTransforminvBill invTransforminvBill = new InvTransforminvBill();
        invTransforminvBill.setIsTransform("N");//一般库存转项目
        invTransforminvBill.setvBillNo(commonService.getNo("HZ"));
        invTransforminvBill.setvProjectNo(param.getvProjectNo());
        invTransforminvBill.setCreateBy(commonService.getUsername());
        invTransforminvBill.setCreateTime(DateUtils.getNowDate());
        invTransforminvBillMapper.insertInvTransforminvBill(invTransforminvBill);

        //反查Id
        invTransforminvBill.setCreateBy(null);
        invTransforminvBill = this.invTransforminvBillMapper.selectInvTransforminvBillList(invTransforminvBill).get(0);
        Long nMainId = invTransforminvBill.getId();

        //拆先入先出
        List<InvTransforminvBillItem> list = new ArrayList<>();

        InvInventoryInfo invInventoryInfo = new InvInventoryInfo();
        BeanUtils.copyBeanProp(invInventoryInfo,param);
        invInventoryInfo.setvBuyNo(null);
        invInventoryInfo.setvProjectNo(null);
        List<InvInventoryInfo> invInventoryInfos = this.invInventoryInfoMapper.selectInvInventoryInfoListOrderByLotyiban(invInventoryInfo);
        BigDecimal nQty = BigDecimal.ZERO;
        BigDecimal nNeedQty = param.getnQty().subtract(param.getnInvQty());
        if (param.getnYibanInvQty().compareTo(nNeedQty) >= 0){
            nQty = nNeedQty;
        }else{
            nQty = param.getnYibanInvQty();
        }
        BigDecimal nThisQty = nQty;
        for (InvInventoryInfo info:invInventoryInfos
        ) {
            if(nQty.compareTo(BigDecimal.ZERO)>0){
                InvTransforminvBillItem invTransforminvBillItem = new InvTransforminvBillItem();
                BeanUtils.copyBeanProp(invTransforminvBillItem,param);
                if (nQty.compareTo(info.getnQty())>0){
                    nQty = nQty.subtract(info.getnQty());
                    invTransforminvBillItem.setnQty(info.getnQty());
                }else{
                    invTransforminvBillItem.setnQty(nQty);
                    nQty = nQty.subtract(nQty);
                }
                invTransforminvBillItem.setnPrice(info.getnPrice());
                invTransforminvBillItem.setvLotNo(info.getvLotNo());
                invTransforminvBillItem.setnMainId(nMainId);
                invTransforminvBillItem.setvWHId(info.getvWHId());
                invTransforminvBillItem.setvAreaId(info.getvAreaId());
                invTransforminvBillItem.setvLocationId(info.getvLocationId());
                invTransforminvBillItem.setvContractNo(info.getvContractNo());
                invTransforminvBillItem.setvProjectNoOld(info.getvProjectNo());
                invTransforminvBillItem.setvBuyNoOld(info.getvBuyNo());
                list.add(invTransforminvBillItem);
            }
        }
        //改库存,先走一般库存划转出，再走项目库存划转进
        //因为是一般库存转出，所以是项目号和采购单号为null
        for (InvTransforminvBillItem item:list
             ) {
            item.setvProjectNo(null);
            item.setvBuyNo(null);
        }
        commonService.changeInventory(invTransforminvBill,new ArrayList<>(list),"HZOUT");//OUT  划转OUT   划转出

        //出完再原库位入进来
        //因为是一般库存转出，所以是项目号和采购单号为null
        for (InvTransforminvBillItem item:list
        ) {
            item.setvProjectNo(param.getvProjectNo());
            item.setvBuyNo(param.getvBuyNo());

        }
        commonService.changeInventory(invTransforminvBill,new ArrayList<>(list),"HZIN");//OUT  划转OUT   划转出\

        //都处理完成之后保存明细
        for (InvTransforminvBillItem item:list
        ) {
            this.invTransforminvBillItemMapper.insertInvTransforminvBillItem(item);
        }

        //更改采购单明细状态到库存调整完成，并且把已划转的库存数量写过来
        BuyRequestbuyBillItem buyRequestbuyBillItem = this.requestbuyBillItemMapper.selectBuyRequestbuyBillItemById(param.getId());
        buyRequestbuyBillItem.setnInvQty(nThisQty.add(buyRequestbuyBillItem.getnInvQty()));
        buyRequestbuyBillItem.setStatus("2");
        this.requestbuyBillItemMapper.updateBuyRequestbuyBillItem(buyRequestbuyBillItem);
    }

    @Override
    public void handleToPurchase(Long id) {
        BuyRequestbuyBillItem buyRequestbuyBillItem = this.requestbuyBillItemMapper.selectBuyRequestbuyBillItemById(id);
        buyRequestbuyBillItem.setStatus("3");
        buyRequestbuyBillItem.setUpdateTime(DateUtils.getNowDate());
        buyRequestbuyBillItem.setUpdateBy(commonService.getUsername());
        this.requestbuyBillItemMapper.updateBuyRequestbuyBillItem(buyRequestbuyBillItem);
    }
}
